package com.elpebe.sysabk.web.response;

import java.util.ArrayList;
import java.util.List;

/**
 * Factory class to create JSON response format.
 * It relates with ExtJS payload format
 * 
 * @author Heri Parid Pardian
 *
 */
public class JsonResponseFactory {
	
	private static JsonResponseFactory _instance = new JsonResponseFactory(); 
	
	public static Response createErrorResponse(String message){
		return _instance.createResponseMessage(false, message);
	}
	
	public static Response createSuccessResponse(String message){
		return _instance.createResponseMessage(true, message);
	}
	
	public static <E>Response createSuccessResponse(E data){
		return _instance.createSuccessModel(data);
	}

	public class Response {
		
		protected boolean success;
		
		public Response(boolean isSuccess) {
			success = isSuccess;
		}

		public boolean isSuccess() {
			return success;
		}
	}
	
	public class ResponseModel<T> extends Response {
		
		private final T data;
		
		public ResponseModel(T data) {
			super(true);
			this.data = data;
		}
		
		public T getData() {
			return data;
		}
	}

	public class ResponseMessage extends Response {
		
		private final String message;
		private final List<String> data = new ArrayList<String>();
		
		public ResponseMessage(boolean success, String msg) {
			super(success);
			message = msg;
		}
		
		public String getMessage() {
			return message;
		}
		
		public List<String> getData(){
			return data;
		}
	}
	
	private <T>ResponseModel<T> createSuccessModel(T data){
		return new ResponseModel<T>(data);
	}
	
	private ResponseMessage createResponseMessage(boolean success, String message){
		return new ResponseMessage(success, message);
	}
}
